package com.crl.ssc.dic.controller;

import com.cars.util.bean.BasePage;
import com.cars.util.global.GlobalOperaCode;
import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.ssc.dic.dto.DicCityDTO;
import com.crl.ssc.dic.service.DicCityService;
import com.crl.ssc.dic.vo.DicCitySearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description: 字典管理-城市字典API
 * @author:  孔垂云
 * @version: 1.0
 * @date: 2020-08-03 18:38:21
 */
@RestController
@RequestMapping("/dic/city")
@Api(tags = "字典管理-城市字典")
public class DicCityController {

    @Autowired
    private DicCityService dicCityService;

    /**
     * 城市字典列表
     *
     * @param dicCitySearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "城市字典列表")
    public JsonResult list(@RequestBody DicCitySearchVO dicCitySearchVO) {
        int count = dicCityService.count(dicCitySearchVO);
        List<DicCityDTO> list = dicCityService.list(dicCitySearchVO);
        BasePage<List<DicCityDTO>> basePage = new BasePage(count, list);
        return JsonResultUtil.ok(basePage);
    }

    /**
     * 新增城市字典
     *
     * @param dicCityDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增城市字典")
    public JsonResult add(@RequestBody @Valid DicCityDTO dicCityDTO) {
        int flag = dicCityService.add(dicCityDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 修改城市字典
     *
     * @param dicCityDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改城市字典")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody DicCityDTO dicCityDTO) {
        int flag = dicCityService.update(dicCityDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 删除城市字典
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除城市字典")
    public JsonResult delete(@RequestBody Map<String, String> map) {
        String cityCode = map.get("cityCode");
        if (StringUtil.isNullOrEmpty(cityCode)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        int flag = dicCityService.delete(cityCode);
        if (flag == GlobalOperaCode.OPERA_SUCCESS) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 获取单个城市字典
     *
     * @param map
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个城市字典")
    public JsonResult get(@RequestBody Map<String, String> map) {
        String cityCode = map.get("cityCode");
        if (StringUtil.isNullOrEmpty(cityCode)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        DicCityDTO dicCityDTO = dicCityService.get(cityCode);
        return JsonResultUtil.ok(dicCityDTO);
    }
}
